package org.jeecg.modules.middleware.rabbitmq.gen.handler.impl;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.middleware.rabbitmq.gen.EnumGenHandler;
import org.jeecg.modules.middleware.rabbitmq.gen.handler.IGenMqHandle;
import org.jeecg.modules.third.ecloud.sdk.EclouModel;
import org.jeecg.modules.third.ecloud.sdk.EcloudSdkService;
import org.jeecg.modules.wlhy.company.entity.HyCompany;
import org.jeecg.modules.wlhy.company.service.IHyCompanyService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 易云章
 */
@Service
public class YyzHandler implements IGenMqHandle {

    private static final Logger LOGGER = LoggerFactory.getLogger(YyzHandler.class);

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Autowired
    private EcloudSdkService ecloudSdkService;

    @Autowired
    private IHyDriverService hyDriverService;

    @Autowired
    private IHyVehicleService vehicleService;

    @Autowired
    private IHyCompanyService hyCompanyService;

    @Override
    public String getHandlerName() {
        return EnumGenHandler.YYZ_HANDLER.getHandlerName();
    }

    @Override
    public void handle(String channel , String msg) {

        LOGGER.debug("[开始消费REDIS消息队列-签署易云章-数据... ] jsonMessage:{}" ,msg);

        try {

            EclouModel eclouModel =  JSONObject.parseObject(
                    JSONObject.parse(msg).toString(), EclouModel.class);
            HyDriver driver = hyDriverService.getById(eclouModel.getHyDriverId());

            HyCompany hyEnterprise = hyCompanyService.getById(eclouModel.getHyEnterpriseId());

            HyVehicle vehicle = vehicleService.getById(eclouModel.getVehicleId());
            HyShippingNote shippingNote = hyShippingNoteService.getById(eclouModel.getShippingNoteId());


            Object result = ecloudSdkService.autoSign(driver, hyEnterprise, vehicle,shippingNote).getResult();
            JSONObject hetong = (JSONObject) JSONObject.toJSON(result);

            String  contractNum = (String) hetong.get("contractNum");
            String  contractNum2 = (String) hetong.get("contractNum2");

            shippingNote.setContractNum(contractNum);
            shippingNote.setContractNum2(contractNum2);
            shippingNote.setContractType("承运人电子合同");
            shippingNote.setContractStatus("1");
            shippingNote.setContractOr("1");

            hyShippingNoteService.updateById(shippingNote);

        } catch (Exception e) {
            LOGGER.error("[消费REDIS消息队列-签署易云章-数据失败，失败信息:{}]", e.getMessage());

        }
    }
}
